Static Keyword in C++ 您所在的位置:网站首页 Using Static Keyword in JavaScript Static Keyword in C++

Static Keyword in C++

2024-05-14 22:21| 来源: 网络整理| 查看: 265

Static Keyword in C++ C++ProgrammingServer Side Programming

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.

Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.

Here is the syntax of static keyword in C++ language,

static datatype variable_name = value; // Static variable static return_type function_name { // Static functions ... }

Here,

datatype − The datatype of variable like int, char, float etc.

variable_name − This is the name of variable given by user.

value − Any value to initialize the variable. By default, it is zero.

return_type − The datatype of function to return the value.

function_name − Any name to the function.

Here is an example of static variable in C++ language,

Example

Live Demo

#include using namespace std; class Base { public : static int val; static int func(int a) { cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有